package com.finals.finalsscreen.util;

import java.io.File;
import java.io.IOException;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;

import com.finals.finalsscreen.app.BaseApplication;

public class MusicPlayer implements OnPreparedListener {

	BaseApplication mApp;

	MediaPlayer mediaPlayer;

	public MusicPlayer(BaseApplication mApp) {
		this.mApp = mApp;
		mediaPlayer = new MediaPlayer();
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		mediaPlayer.setOnPreparedListener(this);
	}

	public void PlayMusic(File path) {
		this.mediaPlayer.reset();
		try {
			mediaPlayer.setDataSource(path.getAbsolutePath());
		} catch (IOException e) {
			e.printStackTrace();
		}
		mediaPlayer.prepareAsync();
	}

	@Override
	public void onPrepared(MediaPlayer mediaPlayer) {
		this.mediaPlayer.start();
	}

	public void Stop() {
		if (this.mediaPlayer != null) {
			if (this.mediaPlayer.isPlaying()) {
				this.mediaPlayer.pause();
				this.mediaPlayer.stop();
			}
			this.mediaPlayer.reset();
		}
	}

	public void onDestory() {
		Stop();
		if (this.mediaPlayer != null) {
			this.mediaPlayer.release();
		}
	}

}
